package top.codestudy.servicetrain;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "MainActivity";
    private Button mBtnGoSencond;
    private Button mBtnStartService;
    private Button mBtnStopService;
    private Button mBtnBindService;
    private Button mBtnUnbindService;

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MyBinder service1 = (MusicService.MyBinder) service;
            Log.i(TAG, "onServiceConnected: ComponentName ==> " + name.toString());
            Log.i(TAG, "onServiceConnected: IBinder => " + service + ";MyBinder中获取service实例 = " + service1.getServiceName() );
            ((MusicService.MyBinder) service).name = "dd";
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected: ");
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnGoSencond = findViewById(R.id.btn_go_sencond);

        mBtnStartService = findViewById(R.id.btn_start_service);
        mBtnStopService = findViewById(R.id.btn_stop_service);
        mBtnBindService = findViewById(R.id.btn_bind_service);
        mBtnUnbindService = findViewById(R.id.btn_unbind_service);
        mBtnGoSencond.setOnClickListener(this);
        mBtnStartService.setOnClickListener(this);
        mBtnStopService.setOnClickListener(this);
        mBtnBindService.setOnClickListener(this);
        mBtnUnbindService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_go_sencond:
                Intent intent = new Intent(this,SencondActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_service:
                startMyService();
                break;
            case R.id.btn_stop_service:
                stopMyService();
                break;
            case R.id.btn_bind_service:
                bindMyService();
                break;
            case R.id.btn_unbind_service:
                unbindMyService();
                break;

        }
    }

    private void startMyService() {
        Intent intent = new Intent(this,MusicService.class);
        startService(intent);
    }

    private void stopMyService() {
        Intent intent = new Intent(this,MusicService.class);
        stopService(intent);
    }

    private void bindMyService() {
        Intent intent = new Intent(this,MusicService.class);
        intent.putExtra("name","chencheng | "+ System.currentTimeMillis());
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void unbindMyService() {
        unbindService(serviceConnection);
    }


}